package org.example.join;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

public class WindowIntervalJoinLatenessDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<Tuple2<String, Integer>> ds1 = env.socketTextStream("localhost", 8888)
                .map(t -> Tuple2.of(t.split(",")[0], Integer.parseInt(t.split(",")[1])))
                .returns(Types.TUPLE(Types.STRING, Types.INT))
                .assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((t, l) -> t.f1 * 1000));

        SingleOutputStreamOperator<Tuple3<String, Integer, Integer>> ds2 = env.socketTextStream("localhost", 9999)
                .map(t -> Tuple3.of(t.split(",")[0], Integer.parseInt(t.split(",")[1]), Integer.parseInt(t.split(",")[2])))
                .returns(Types.TUPLE(Types.STRING, Types.INT, Types.INT))
                .assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple3<String, Integer, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((t, l) -> t.f1 * 1000));

        OutputTag<Tuple2<String, Integer>> ksOutTag1 = new OutputTag<>("late1", Types.TUPLE(Types.STRING, Types.INT));
        OutputTag<Tuple3<String, Integer, Integer>> ksOutTag2 = new OutputTag<>("late2", Types.TUPLE(Types.STRING, Types.INT, Types.INT));
        //to do interval join
        //1 分别做Key by j
        KeyedStream<Tuple2<String, Integer>, String> keyedStream1 = ds1.keyBy(d1 -> d1.f0);
        KeyedStream<Tuple3<String, Integer, Integer>, String> keyedStream2 = ds2.keyBy(d2 -> d2.f0);
        //2 做interval join
        SingleOutputStreamOperator<String> process = keyedStream1
                .intervalJoin(keyedStream2)
                .between(Time.seconds(-2), Time.seconds(2))
                .sideOutputLeftLateData(ksOutTag1)
                .sideOutputRightLateData(ksOutTag2)
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {
                    //两条流的数据匹配上，才会去调用这个方法
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple3<String, Integer, Integer> right, ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                        out.collect("left:" + left + "===>right:" + right);
                    }
                });
        process.print();
        process.getSideOutput(ksOutTag1).printToErr("ksOutTag1");
        process.getSideOutput(ksOutTag2).printToErr("ksOutTag2");
        env.execute("WindowJoinDemo");
    }
}
